package org.whh.automatic.util;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
 * StepDialogUIUtil
 * <p>
 * 用于封装StepDialog中复杂UI组件的创建逻辑，
 * 包括主面板、标题面板、内容面板、分区边框等。
 * 便于主对话框类结构简洁、易于维护。
 */
public class StepDialogUIUtil {
    /**
     * 创建主面板，带白色背景和抗锯齿渲染。
     *
     * @return 主面板JPanel
     */
    public static JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout(0, 0)) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.dispose();
            }
        };
        mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        return mainPanel;
    }

    /**
     * 创建带渐变背景的标题面板。
     *
     * @param width         宽度
     * @param height        高度
     * @param gradientStart 渐变起始色
     * @param gradientEnd   渐变结束色
     * @param title         标题文本
     * @return 标题面板JPanel
     */
    public static JPanel createTitlePanel(int width, int height, Color gradientStart, Color gradientEnd, String title) {
        JPanel titlePanel = new JPanel(new BorderLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                GradientPaint gp = new GradientPaint(0, 0, gradientStart, 0, getHeight(), gradientEnd);
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.dispose();
            }
        };
        titlePanel.setPreferredSize(new Dimension(width, height));
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
        titlePanel.add(titleLabel, BorderLayout.WEST);
        return titlePanel;
    }

    /**
     * 创建带标题的分区边框。
     *
     * @param title       边框标题
     * @param borderColor 边框颜色
     * @param titleColor  标题颜色
     * @return 复合边框CompoundBorder
     */
    public static CompoundBorder createSectionBorder(String title, Color borderColor, Color titleColor) {
        TitledBorder titledBorder = BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(borderColor),
                title,
                TitledBorder.LEFT,
                TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 14),
                titleColor);
        return BorderFactory.createCompoundBorder(
                titledBorder,
                BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    /**
     * 创建带样式的单选按钮。
     *
     * @param text       按钮文本
     * @param background 背景色
     * @return 配置好的JRadioButton
     */
    public static JRadioButton createStyledRadioButton(String text, Color background) {
        JRadioButton radio = new JRadioButton(text);
        radio.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        radio.setBackground(background);
        radio.setFocusPainted(false);
        return radio;
    }

    /**
     * 创建带样式的按钮。
     *
     * @param text       按钮文本
     * @param background 按钮背景色
     * @param hover      按钮悬停色
     * @param textColor  按钮文字色
     * @return 配置好的JButton
     */
    public static JButton createStyledButton(String text, Color background, Color hover, Color textColor) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                if (getModel().isPressed()) {
                    g2.setColor(background.darker());
                } else if (getModel().isRollover()) {
                    g2.setColor(hover);
                } else {
                    g2.setColor(background);
                }
                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 8, 8);
                g2.setColor(textColor);
                int textWidth = g2.getFontMetrics().stringWidth(getText());
                int textHeight = g2.getFontMetrics().getHeight();
                g2.drawString(getText(), (getWidth() - textWidth) / 2,
                        (getHeight() - textHeight) / 2 + g2.getFontMetrics().getAscent());
                g2.dispose();
            }
        };
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        button.setForeground(textColor);
        button.setBackground(background);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        return button;
    }
} 